<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/21
 * Time: 9:15
 */

namespace App\GraphQL\Mutations;

use Closure;
use App\User;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ResolveInfo;
use Rebing\GraphQL\Support\Mutation;

class UpdateUserPasswordMutation extends Mutation
{

    protected $attributes = [
        'name' => 'updateUserPassword'
    ];

    public function type(): Type
    {
        return \GraphQL::type('users');
    }

    public function args(): array
    {
        return [
            'id' => [
                'name' => 'id',
                'type' => Type::nonNull(Type::string())
            ],
            'password' => [
                'name' => 'password',
                'type' => Type::nonNull(Type::string())
            ]
        ];
    }



    public function resolve($root, $args, $context, ResolveInfo $resolveInfo, Closure $getSelectFields)
    {


    $s = <<<TEST
    
      //注意最外面不需要大括号, 小写的mutation
      mutation{
        updateUserPassword(id: "3", password: "123"){
          id
          name
        }
      }
    
TEST;

        $user = User::find($args['id']);
        if(!$user) {
            return null;
        }

        $user->password = bcrypt($args['password']);
        $user->save();
        return $user;
    }
}
